
import router from "../router"
const localData=localStorage.getItem('pz_v3pz')
const state =localData?localData.menu: {
    isCollapse: false,
    selectMenu: [],
    routerList:[],
    menuActive:'1-1'
}
const mutations = {
    collapseMenu(state) {
        state.isCollapse = !state.isCollapse
    },
    addMenu(state, payload) {
        //  对数据进行去重，如果不存在就传入,-1就是不存在
        if (state.selectMenu.findIndex(item => item.path === payload.path) === -1) {
            state.selectMenu.push(payload)
        }
    },
    closeMenu(state,payload){
        // 找到点击数据的索引
        const index=state.selectMenu.findIndex(val=>val.name===payload.name)
        // 通过索引删除数组指定元素,1是删除的个数
        state.selectMenu.splice(index,1)
    },
     updateMenuActive(state,payload){
        state.menuActive=payload
    },
    dynamicMenu(state,payload){
        // 对数据进行处理
    // vite动态引入， 通过glob导入文件
    const modules=import.meta.glob('../views/**/**/*.vue')
    console.log(modules)
        function routerSet(router){
            // 判断没有子菜单，拼接路由数据
            router.forEach(route => {
                 if(!route.children){
                   const url=`../views${route.meta.path}/index.vue`
                //    拿到获取的vue组件
                   route.component=modules[url]
                 }else{
                    routerSet(route.children)
                 }
            });
           
        }
        routerSet(payload)
        //拿到完整的路由数据
        state.routerList=payload
    },
   
}
export default {
    state,
    mutations
}
